<template>
  <div class="settings-page">
    <div class="container page">
      <div class="row">
        <div class="col-md-6 offset-md-3 col-xs-12">
          <h1 class="text-xs-center">设置</h1>

          <form>
            <fieldset>
              <fieldset class="form-group">
                <input
                  v-model="user.image"
                  class="form-control"
                  type="text"
                  placeholder="URL of profile picture"
                />
              </fieldset>
              <!-- <fieldset class="form-group">
                <input
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Your Name"
                />
              </fieldset> -->
              <fieldset class="form-group">
                <textarea
                  v-model="user.bio"
                  class="form-control form-control-lg"
                  rows="8"
                  placeholder="Short bio about you"
                ></textarea>
              </fieldset>
              <fieldset class="form-group">
                <input
                  v-model="user.email"
                  class="form-control form-control-lg"
                  type="text"
                  placeholder="Email"
                />
              </fieldset>
              <fieldset class="form-group">
                <input
                  disabled
                  class="form-control form-control-lg"
                  type="password"
                  placeholder="Password"
                />
              </fieldset>
              <button
                class="btn btn-lg btn-primary pull-xs-right"
                @click.prevent="updateUser"
              >
                保存
              </button>
            </fieldset>
          </form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
/*eslint-disable */
import { mapState, mapMutations } from 'vuex'
import { updateUser } from './api/index'
const Cookie = process.client ? require('js-cookie') : null
export default {
  middleware: 'auth',
  created() {
    console.log(this.userInfo)
    this.user.email = this.userInfo.email
    this.user.bio = this.userInfo.bio
    this.user.image = this.userInfo.image
  },
  computed: {
    ...mapState({
      userInfo: 'user'
    })
  },
  methods: {
    ...mapMutations(['setUser']),
    async updateUser() {
      const res = await updateUser(this.user)
      console.log(res)
      if (res.status === 200) {
        this.$message.info('更改成功!')
        Cookie.set('user', res.data.user)
        this.setUser(res.data.user)
      }
    }
  },
  data() {
    return {
      user: {
        email: '',
        bio: '',
        image: ''
      }
    }
  }
}
</script>

<style scoped></style>
